<?php

class Common_DataTable_Model_DatabaseObject extends Common_DataTable_Model_Abstract {
    
    private $obj_data_object = null;
    
    public function __construct(Common_DataGateway_Database_Object $obj_data_object, $str_sql) {
        
        parent::__construct();
        
        $this->__addMixin(new Common_DataTable_Model_SortMeta(), 'sortmeta');
        
        $this->setDataObject($obj_data_object);
        $this->setSql($str_sql);
    }
    
    protected function setDataObject(Common_DataGateway_Database_Object $obj_data_object) {
        $this->obj_data_object = $obj_data_object;
    }
    
    protected function getDataObject() {
        return $this->obj_data_object;
    }
    
    protected function getSql() {
        return $this->getDataObject()->getSql();
    }
    
    public function setSql($str_sql) {
        $this->getDataObject()->setSql($str_sql);
    }
    
    public function getData() {
        return $this->getDataObject()->getData();
    }

    public function hasData() {
        $bln = false;
        
        $str_sql = $this->getSql();
        $str_sql_count = preg_replace('/(SELECT )(.*)( FROM .*)/Umi', '$1 COUNT(1) AS count $3', $str_sql);  
        
        $this->getDataObject()->setSql($str_sql_count);
        $obj_results = $this->getDataObject()->getData();
        
        $int_count = $obj_results->fetchColumn();
        
        $this->getDataObject()->setSql($str_sql);
        
        return ($int_count > 0);
    }
    
}
